package org.nhindirect.stagent;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import javax.mail.internet.InternetAddress;

import org.nhindirect.stagent.trust.TrustEnforcementStatus;

import java.security.cert.X509Certificate;

public class NHINDAddressCollection extends ArrayList<NHINDAddress> 
{
    public static final TrustEnforcementStatus DefaultMinTrustStatus = TrustEnforcementStatus.Success_Offline;
    
    public NHINDAddressCollection()
    {
    }
    
    public Collection<X509Certificate> getCertificates()
    {
    	Collection<X509Certificate> certs = new ArrayList<X509Certificate>();
        
    	for (NHINDAddress add : this)
    		if (add.hasCertificate())
    			certs.add(add.getCertificate());
    	
    	return certs;
    }
    
    public X509Certificate getFirstCertificate()
    {
    	for (NHINDAddress add : this)
    		if (add.hasCertificate())
    			return add.getCertificate();
    	
    	return null;
    }
            
    /// <summary>
    /// Get the trustworthy members of this collection
    /// </summary>
    public Collection<NHINDAddress> getTrusted()
    {
        return this.getTrusted(NHINDAddressCollection.DefaultMinTrustStatus);
    }

    public Collection<NHINDAddress> getTrusted(TrustEnforcementStatus minTrustStatus)
    {
    	Collection<NHINDAddress> adds = new ArrayList<NHINDAddress>();
    	
    	for (NHINDAddress add : this)
    		if (add.isTrusted(minTrustStatus))
    			adds.add(add);
    			
    	return adds;
    }
    
    public Collection<NHINDAddress> GetUntrusted()
    {
        return this.getUntrusted(NHINDAddressCollection.DefaultMinTrustStatus);
    }

    public Collection<NHINDAddress> getUntrusted(TrustEnforcementStatus minTrustStatus)
    {
    	Collection<NHINDAddress> adds = new ArrayList<NHINDAddress>();
    	
    	for (NHINDAddress add : this)
    		if (!add.isTrusted(minTrustStatus))
    			adds.add(add);
    			
    	return adds;    	
    }
    
    /// <summary>
    /// Does this collection of NHINDAddress contain only trustworthy addresses? 
    /// </summary>
    /// <returns></returns>        
    public boolean isTrusted()
    {
        return this.isTrusted(NHINDAddressCollection.DefaultMinTrustStatus);
    }

    public boolean isTrusted(TrustEnforcementStatus minTrustStatus)
    {
    	for (NHINDAddress add : this)
    		if (!add.isTrusted(minTrustStatus))
    			return false;
    	
    	return true;    	
    }
    
    public void removeUntrusted()
    {
        this.removeUntrusted(NHINDAddressCollection.DefaultMinTrustStatus);
    }
    
    public void removeUntrusted(TrustEnforcementStatus minTrustStatus)
    {
        // Remove anybody who is not trusted
    	for (int i = this.size() - 1; i >=0; --i)
    		if (!this.get(i).isTrusted(minTrustStatus))
    			this.remove(i);
    }
    
    public Collection<InternetAddress> toInternetAddressCollection()
    {
    	Collection<InternetAddress> retVal = new ArrayList<InternetAddress>();
    	
    	retVal.addAll(this);
    	
    	return retVal;
    	
    }
    
    static NHINDAddressCollection create(Collection<NHINDAddress> source)
    {
        NHINDAddressCollection addresses = new NHINDAddressCollection();
        addresses.addAll(source);
        
        return addresses;
    }   
}
